package com.six.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Goodscart;
import com.six.core.entity.Order;
import com.six.core.vo.Result;
import com.six.order.service.GoodsCartService;
import com.six.order.dao.GoodscartMapper;
import com.six.order.vo.AddGoodsCartVo;
import com.six.order.vo.dto.GoodsCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 汪鹏
* @description 针对表【tb_goodscart】的数据库操作Service实现
* @createDate 2023-03-03 15:02:29
*/
@Service
public class GoodsCartServiceImpl extends ServiceImpl<GoodscartMapper, Goodscart> implements GoodsCartService {

    @Autowired
    private GoodscartMapper goodscartMapper;
    /** 获取用户购物中物品的条数，根据userid进行查询 */
    @Override
    public Result getGoodsCartNum(Integer userid) {
        QueryWrapper<Goodscart> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as sum");
        queryWrapper.eq("userid",userid);
        Map<String, Object> map = this.getMap(queryWrapper);
        return new Result(200,"查询成功",map.get("sum"));
    }

    /** 通过userid获取当前用户的购物车列表 */
    @Override
    public Result getListByUserid(Integer userid) {
        List<GoodsCartVo> list = goodscartMapper.getListByUserid(userid);
        return new Result(200,"查询成功",list);
    }

    /** 根据id删除购物车的单个商品 */
    @Override
    public Result deleteOneById(Integer id) {
        int i = goodscartMapper.deleteById(id);
        return new Result(200,"删除成功");
    }

    /** 用户批量删除购物车的商品 */
    @Override
    public Result deleteByIds(List<Integer> ids) {
        int i = goodscartMapper.deleteBatchIds(ids);
        return new Result(200,"批量删除成功");
    }

    /** 点击加减按钮的时候修改购物车中的数量 */
    @Override
    public Result updateNum(Integer id, Integer goodsNum,Double totalPrice) {
        UpdateWrapper<Goodscart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("goodsnum",goodsNum)
                .set("totalprice",totalPrice)
                .eq("id",id);
        int i = goodscartMapper.update(new Goodscart(), updateWrapper);
        return new Result(200,"修改数量成功");
    }

    /** 向购物车中添加商品，首先根据userid，goodsid进行数据库查询，
     *  判断这个物品是否在数据库中，若是在就增加数量，不在就添加进购物车。*/
    @Override
    public Result addGoodsCart(AddGoodsCartVo addGoodsCartVo) {
        //首先当前用户的购物车里有没有该商品
        QueryWrapper<Goodscart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",addGoodsCartVo.getUserid())
                    .eq("goodsid",addGoodsCartVo.getGoodsId());
        Goodscart goodscart = goodscartMapper.selectOne(queryWrapper);
        //如果购物车不存在该商品，向购物车添加商品
        if (goodscart == null){
            Goodscart goodscart1 = new Goodscart();
            goodscart1.setGoodsid(addGoodsCartVo.getGoodsId());
            goodscart1.setGoodsnum(addGoodsCartVo.getGoodsNum());
            goodscart1.setUserid(addGoodsCartVo.getUserid());
            goodscart1.setTotalprice(addGoodsCartVo.getTotal());
            int i = goodscartMapper.insert(goodscart1);
            return new Result(200,"添加购物车成功");
        }else {
            //如果购物车已经存在该商品修改购物车的商品数量与总价
            UpdateWrapper<Goodscart> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("goodsnum",goodscart.getGoodsnum()+ addGoodsCartVo.getGoodsNum())
                    .set("totalPrice",goodscart.getTotalprice()+ addGoodsCartVo.getTotal())
                    .eq("id",goodscart.getId());
            int i = goodscartMapper.update(new Goodscart(), updateWrapper);
            return new Result(200,"添加购物车成功");
        }
    }

    @Override
    public Integer deleteInfo(List<Order> orders) {
        for (Order order : orders) {
            QueryWrapper<Goodscart> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goodsid",order.getGoodsid())
                    .eq("userid",order.getUserid());
            goodscartMapper.delete(queryWrapper);
        }
        return 1;
    }

}




